<?php
function betin ($bettype, $num, $rate1, $rate, $money, $betcounts, $prot_start, $stime, $idisk)
{
    global $curruser;
    global $lotto_id;
    $numbers = $money . "|" . $rate . "|" . $betcounts . "|" . implode(",", $num);
    $fields = array();
    $fields['user_id'] = $curruser['id'];
    $fields['lotto_id'] = $lotto_id;
    $fields['createtime'] = $stime;
    $fields['bettype'] = $bettype;
    $fields['numbers'] = "'{$numbers}'";
    $fields['amount'] = $money * $betcounts;
    $fields['line_nums'] = "'" . implode(",", $num) . "'";
    $fields['unitmoney'] = $money;
    $fields['rate'] = $rate1;
    $fields['payoff'] = round($money * ( $rate1 - 1 ));
    $fields['disk'] = $idisk;
    $u2 = get_user($curruser['prev_id']);
    $u3 = get_user($u2['prev_id']);
    $u4 = get_user($u3['prev_id']);
    $u5 = get_user($u4['prev_id']);
    $fields['acl2'] = $u2['id'];
    $fields['acl3'] = $u3['id'];
    $fields['acl4'] = $u4['id'];
    $fields['acl5'] = $u5['id'];
    $a1 = GetBack($curruser['id'], $fields['bettype'], $idisk, 1);
    $a2 = GetBack($u2['id'], $fields['bettype'], $idisk, 2);
    $a3 = GetBack($u3['id'], $fields['bettype'], $idisk, 3);
    $a4 = GetBack($u4['id'], $fields['bettype'], $idisk, 4);
    $a5 = GetBack($u5['id'], $fields['bettype'], $idisk, 5);
    $fields['back1'] = $a1;
    $fields['back2'] = $a2;
    $fields['back3'] = $a3;
    $fields['back4'] = $a4;
    $fields['back5'] = $a5;
    $a6 = GetBack($u5['supe_account'], $fields['bettype'], $idisk, 255);
    $fields['back6'] = $a6;
    $protmod = $u5['protmod'];
    $fields['mpercent'] = $u2['mpercent'];
    $fields['percent2'] = $u2['percent0'];
    $fields['percent3'] = min($u3['percent1'], $u4['mpercent'] - $fields['mpercent'] - $fields['percent2']);
    $fields['percent4'] = min($u4['percent1'], $u5['mpercent'] - $fields['mpercent'] - $fields['percent2'] - $fields['percent3']);
    $fields['upercent'] = 100 - $fields['mpercent'] - $fields['percent2'] - $fields['percent3'] - $fields['percent4'];
    if ($fields['percent3'] < 0 || $fields['percent4'] < 0 || $fields['upercent'] < 0)
    {
        err("占成参数设置错误，注单无法存档！");
    }
    else
    {
        $fields['r2'] = $money * $fields['percent2'] / 100;
        $fields['r3'] = $money * $fields['percent3'] / 100;
        $fields['r4'] = $money * $fields['percent4'] / 100;
        $fields['r6'] = $money * $fields['upercent'] / 100;
        $fields['ip'] = "'" . getIP() . "'";
        $newid = db_insert("bet", $fields);
        if (!$newid)
        {
            return false;
        }
		$rs=getdb("select user_id from k_user_bet where user_id='".$curruser['id']."' and lotto_id='$lotto_id'");
		if(!$rs->eof)
			getdb("update k_user_bet set amount_6h=amount_6h+".$money." where user_id='".$curruser['id']."' and lotto_id='$lotto_id'");
		else
			getdb("insert into k_user_bet set amount_6h=amount_6h+".$money." ,user_id='".$curruser['id']."',lotto_id='$lotto_id'");
        foreach ($num as $numVal)
        {
            check_autorate($lotto_id, $bettype, $numVal, $money * ( $fields['upercent'] / 100 ));
        }
        if ($prot_start == 1 && $protmod == 1)
        {
            $tmpnum = implode(",", $num);
            auto_prots(2, $bettype, $tmpnum, $rate, $u2, $u3, $u4, $u5, $stime, $betcounts);
            auto_prots(3, $bettype, $tmpnum, $rate, $u2, $u3, $u4, $u5, $stime, $betcounts);
            auto_prots(4, $bettype, $tmpnum, $rate, $u2, $u3, $u4, $u5, $stime, $betcounts);
            auto_prots(5, $bettype, $tmpnum, $rate, $u2, $u3, $u4, $u5, $stime, $betcounts);
        }
        return true;
    }
}

if (!defined("IN_OP"))
{
    exit("You can't access this file directly...");
}
if (!isset($_POST['type']))
{
    account_err("错误的参数！");
}
$bettype = intval($_POST['type']);
$bettypeArr = array(29, 39, 40, 41, 42, 43, 44, 45);
if (!in_array($bettype, $bettypeArr))
{
    account_err("错误的参数(type)！");
}
if (!isset($_POST['num1']))
{
    account_err("错误的参数(num1)！");
}
$num1 = $_POST['num1'];
if ($num1 == "")
{
    account_err("请选择号码！");
}
$num_r1 = explode(",", $num1);
$normal = !isset($_POST['num2']);
$num2 = "";
$num_r2 = array();
if (!$normal)
{
    $num2 = $_POST['num2'];
    if ($num1 == "")
    {
        account_err("错误的参数(num2)！");
    }
    $num_r2 = explode(",", $num2);
}
$ui = new UI();
$conf = new Config();
$count_num = 0;
switch ($bettype)
{
    case 29 :
        $count_num = 5;
        break;
    case 39 :
        $count_num = 6;
        break;
    case 40 :
        $count_num = 7;
        break;
    case 41 :
        $count_num = 8;
        break;
    case 42 :
        $count_num = 9;
        break;
    case 43 :
        $count_num = 10;
        break;
    case 44 :
        $count_num = 11;
        break;
    case 45 :
        $count_num = 12;
        break;
}
if ($normal && count($num_r1) != $count_num)
{
    account_err("必须选择 {$count_num} 个号码！");
}
$tpl = new Template2();
$tpl->load(array("main" => "proc12.htm"));
$tpl->set("account", $curruser['account']);
if ($curruser['enabled'] == 0 || $curruser['enabled'] == 2)
{
    $ui->showErrorPage("<p class=error>帐号已被暂停使用</p>");
    exit();
}
$rs = getdb("select * from lotto where open>='" . date("Y-m-d") . "' order by open");
if ($rs->eof)
{
    $ui->showErrorPage("<p class=error>目前没有可以下注期数！</p>");
    exit();
}
$open = $rs->f['open'];
$round = $rs->f['round'];
$lotto_id = $rs->f['id'];
$prot_start = $conf->get("protect_start");
$stopbet = $conf->get("linebet");
if (date("Y-m-d") < $open)
{
    exit();
}
else if (date("Y-m-d") == $open)
{
    if ($stopbet <= date("Hi"))
    {
        $ui->showErrorPage("<p>下注期数：" . d3($round) . "<br>开奖日：" . $open . "</p>" . "<p class=error>已经截止下注！</p>");
        exit();
    }
    else if (date("Hi") <= $conf->get("startbet"))
    {
        exit();
    }
}
$tpl->set("open", $open);
$tpl->set("round", d3($round));
$auto_num = array();
$auto_down = 0;
$auto_count = 0;
$i = 0;
while ($i < 10)
{
    $nn = $conf->get("autorate_b29_num" . $i);
    if (substr_count($nn, ",") == 4)
    {
        $auto_num[$auto_count] = explode(",", $nn);
        ++$auto_count;
    }
    ++$i;
}
$auto_down = $conf->get("autorate_b29_down");
$tpl->set("lotto_id", $lotto_id);
$tpl->set("account", $curruser['account']);
$tpl->set("amount", $curruser['amount']);
$chktime2 = 0;
$chk_rs = getdb("!!select max(createtime) from bet where lotto_id=" . $lotto_id . " and user_id=" . $curruser['id'] . " and bettype=" . $bettype . " and rame=0");
if (!$chk_rs->eof)
{
    $chktime2 = $chk_rs->f[0];
}
$chk_rs->close();
$chktime1 = systime();
if (intval($chktime1) - intval($chktime2) < 12)
{
    account_err("请於 " . ( 12 - ( intval($chktime1) - intval($chktime2) ) ) . "秒後才继续下注！");
}
$rs1 = getdb("select amount_6h from k_user_bet where user_id=" . $curruser['id'] . " and lotto_id=" . $lotto_id);
$usedamount = $rs1->f[0] ? $rs1->f[0] : 0;
$rs1->close();
$tpl->set("usedamount", $usedamount);
$maxMonTemp = 0;
foreach ($num_r1 as $val)
{
    $val = str_pad($val, 2, "0", STR_PAD_LEFT);
    $rs1 = getdb("!!select sum(amount) from bet where bettype={$bettype} and user_id=" . $curruser['id'] . " and lotto_id={$lotto_id} and line_nums like '%{$val}%' and rame=0");
    $maxMonTemp = max($rs1->f[0], $maxMonTemp);
}
$betamount = $rs1->f[0] ? $rs1->f[0] : 0;
$betamount = max($betamount, $maxMonTemp);
$tpl->set("betamount", $betamount);
$rs1->close();
$betforms = "";
$i = 0;
while ($i < count($num_r1))
{
    $num_r1[$i] = d2($num_r1[$i]);
    ++$i;
}
$num1 = implode(",", $num_r1);
$betforms .= "<input type=hidden name=\"num1\" value=\"" . $num1 . "\">\n";
$tpl->set("betforms", $betforms);
$tpl->set("type", $bettype);
$idisk = getUserValidDisk($curruser);
$disk = $g_disk_code[$idisk];
$tpl->set("diskStr", strtoupper($disk) . "盘");
$rs = getdb("select * from rate29 where lotto_id={$lotto_id} AND bettype={$bettype} AND disk = " . $idisk . " LIMIT 1");
if ($rs->eof)
{
    account_err("系统目前维护中，请稍後！");
}
$rate29Arr = array();
foreach ($num_r1 as $val)
{
    $rate29Arr[] = floatval($rs->f["r" . d2($val)]);
}
sort($rate29Arr);
$rate1 = $rate29Arr[0];
$rate = $rate29Arr[0];
if (0 < $auto_count)
{
    $chk_mode = 0;
    $i = 0;
    while ($i < $auto_count)
    {
        if (count(array_intersect($auto_num[$i], $num_r1)) == 5)
        {
            ++$chk_mode;
            break;
        }
        ++$i;
    }
    if (0 < $chk_mode)
    {
        $rate1 -= $auto_down;
    }
}
$betcontent = "";
$chk_betnum = 0;
$err = 0;
if ($normal)
{
    $r1count = count($num_r1);
    $i = 0;
    while ($i < $r1count)
    {
        if (intval($num_r1[$i]) < 1 || 49 < intval($num_r1[$i]))
        {
            ++$err;
            break;
        }
        ++$i;
    }
    if (0 < $err)
    {
        write_exceptional_log($curruser['id'], $curruser['acl'], $lotto_id, "玩法(" . $bettype . ")，使用不正常方式下注（" . implode(",", $num_r1) . "）");
        account_err("请勿以不正常方式下注，你的 IP 已经被记录并已经通知你的上层管理了！");
    }
}
$ratechange = $rate == $rate1 ? false : true;
if ($ratechange)
{
    $tpl->set("message", "<tr><td colspan=2 bgcolor=red align=center><font color=white>赔率已经变动！</font></td></tr>");
}
$tpl->set("rates", $rate1);
$betcontents = "<font color=red>" . $g_betname[$bettype] . "</font> @  <font color=red id=rate>" . $rate1 . "</font><br>";
$betcontents .= "[ " . $num1 . " ]<br>合计<font color=red><b id=nc>1</b></font>组，存成<font color=red><b>1</b></font>笔";
$tpl->set("betcontent", $betcontents);
$betmin = $conf->get("betmin");
$tpl->set("min", $betmin);
$betmax = $conf->get("betmax");
$tpl->set("max", $betmax);
$tpl->set("max_str", $betmax);
$betname = $g_betname[$bettype];
$tpl->set("betname", $betname);
if ($rate1 + $rate + 0 == 0)
{
    account_err($betname . "没有开放下注！");
}
$rs1 = getdb("select * from amounts where bettype=" . $bettype . " and user_id=" . $curruser['id'] . " LIMIT 1");
$amount1 = $rs1->f['amount1'] - 0;
$amount2 = $rs1->f['amount2'] - 0;
if ($amount1 == 0 || $amount2 == 0)
{
    account_err($betname . "没有开放下注！");
}
$m = $amount2 - $betamount;
$tpl->set("amount1", $amount1);
$tpl->set("amount2", $amount2);
$tpl->set("amount1_str", $amount1);
$tpl->set("amount2_str", $amount2);
$tpl->set("amount3_str", $m);
$usemaxamount = $curruser['usemaxamount'];
$money = isset($_POST['money']) ? intval($_POST['money']) : 0;
if (0 < $money)
{
    if ($money < $betmin)
    {
        account_err("下注金额小於最低限额！");
    }
    if ($amount1 < $money)
    {
        account_err("下注金额超过单注限额！");
    }
    if ($amount2 < $money + $betamount)
    {
        account_err("下注金额超过单项限额！");
    }
    if ($curruser['balance'] < $money)
    {
        account_err("下注总金额超过信用额度！");
    }
    if ($m < $money)
    {
        account_err("下注金总额超过本期限额！");
    }
    if ($conf->get("linebet") <= date("Hi"))
    {
        account_err("已经截止下注！");
    }
    $stime = systime();
    if ($normal && !betin($bettype, $num_r1, $rate1, $rate1, $money, 1, $prot_start, $stime, $idisk))
    {
        err("超过信用额度");
    }
    $payoff = round($money * ( $rate1 - 1 ));
    $tpl->load("succ", "succ.htm");
    $tpl->set("ordertype", $betcontents);
    $tpl->set("money", $money);
    $tpl->set("payoff", $payoff);
    $tpl->set("myscript", "parent.mainFrame.location.reload();");
    $tpl->show("succ");
}
else
{
    $tpl->set("money", "");
    $tpl->show("main");
}
?>